﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XMLGener.Utils;


namespace XMLGener.GUI
{
    /// <summary>
    /// 
    /// </summary>
    public partial class LogForm : Form
    {
        delegate void AddLogCallBack(XLogMessage message);
        delegate void ShowThisFormCallBack();
        /// <summary>
        /// 
        /// </summary>
        public LogForm()
        {
            InitializeComponent();
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            logList.Items.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mess"></param>
        public void addMessage(XLogMessage mess)
        {
            if (this.logList.InvokeRequired)
            {
                AddLogCallBack d = new AddLogCallBack(addMessage);
                this.Invoke(d, new object[] { mess });

            }
            else
            {
                this.logList.Items.Add(mess);

            }
        }

        /// <summary>
        /// Method for showint this form, which can be 
        /// called from different threads.
        /// </summary>
        public void showThisForm()
        {
            if (this.InvokeRequired)
            {
                ShowThisFormCallBack s = new ShowThisFormCallBack(showThisForm);
                this.Invoke(s);
            }
            else 
            {
                this.Show();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void lstBox_DrawItem(object sender,DrawItemEventArgs e)
        {
            if (e.Index < 0)
                return;
            Brush myBrush = Brushes.Black;
            e.DrawBackground();
            e.DrawFocusRectangle();
            
            // Determine the color of the brush to draw message
            XLogMessage mess = (XLogMessage)((ListBox)sender).Items[e.Index];
            switch (mess.type)
            {
                case XLogMessage.LogType.ERROR:
                    myBrush = Brushes.Red;
                    break;
                default:
                    myBrush = Brushes.Black;
                    break;
            }
            //
            // Draw the current item text based on the current 
            // Font and the custom brush settings.
            //
            e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), 
                e.Font, myBrush,e.Bounds);
            //
            // If the ListBox has focus, draw a focus rectangle 
            // around the selected item.
            //
        }

        /// <summary>
        /// Closing event handler. Instead of closing the
        /// hanler hides this form.
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">closing event</param>
        private void LogForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            // cancel hding
            e.Cancel = true;
        }

        /// <summary>
        /// Initialize 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LogForm_Load(object sender, EventArgs e)
        {
            //logList.DrawItem += new DrawItemEventHandler(this.lstBox_DrawItem);
//            XLogger.instance.errorEvent += addMessage;
//            XLogger.instance.logEvent += addMessage;
            
        }
        /*
        private void logList_DrawItem(object sender, DrawItemEventArgs e)
        {
            this.lstBox_DrawItem(sender, e);
        }

        private void logList_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            if (e.Index < 0)
                return;
            XLogMessage mess = (XLogMessage)((ListBox)sender).Items[e.Index];
            
            SizeF headingSize = e.Graphics.MeasureString(mess.ToString(), this.logList.Font, ClientSize.Width);
            e.ItemWidth = (int)(headingSize.Width);
        }*/


    }
}
